package com.carl.zlds.service.impl;

import com.carl.zlds.tool.common.PageBO;
import com.carl.zlds.tool.util.AssertUtil;
import com.carl.zlds.tool.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carl.zlds.dao.BlackAccountMapper;
import com.carl.zlds.model.BlackAccount;
import com.carl.zlds.model.Game;
import com.carl.zlds.pojo.dto.BlackAccountSearchDTO;
import com.carl.zlds.pojo.dto.SaveBlackAccountDTO;
import com.carl.zlds.pojo.vo.BlackAccountVO;
import com.carl.zlds.service.IBlackAccountService;
import com.carl.zlds.service.IGameService;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author carl
 */
@Slf4j
@Service
public class BlackAccountServiceImpl extends ServiceImpl<BlackAccountMapper, BlackAccount> implements IBlackAccountService {

    private final IGameService gameService;

    public BlackAccountServiceImpl(IGameService gameService) {
        this.gameService = gameService;
    }

    @Override
    public void saveBlackAccount(Long id, SaveBlackAccountDTO dto) {
        BlackAccount blackAccount;
        String account = dto.getAccount();
        Long gameId = dto.getGameId();

        Game game = gameService.getOne(new QueryWrapper<Game>().eq("id", gameId).eq("weight", 0));
        AssertUtil.isNull("游戏不存在", game);

        if (id > 0) {
            blackAccount = this.baseMapper.selectOne(new QueryWrapper<BlackAccount>().eq("id", id).eq("weight", 0));
            AssertUtil.isNull("黑号不存在", blackAccount);
            Wrapper<BlackAccount> wrapper = new QueryWrapper<BlackAccount>().eq("account", account)
                    .eq("weight", 0).ne("id", id);
            BlackAccount ba = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNotNull("黑号已存在", ba);
        } else {
            Wrapper<BlackAccount> wrapper = new QueryWrapper<BlackAccount>().eq("id", id)
                    .eq("account", account).eq("weight", 0);
            BlackAccount ba = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNotNull("黑号已存在", ba);
            blackAccount = new BlackAccount();
        }

        blackAccount.setAccount(account);
        blackAccount.setGameId(gameId);

        if (id > 0) {
            this.baseMapper.updateById(blackAccount);
        } else {
            this.baseMapper.insert(blackAccount);
        }
    }

    @Override
    public void deleteBlackAccount(Long id) {
        Wrapper<BlackAccount> wrapper = new QueryWrapper<BlackAccount>().eq("id", id).eq("weight", 0);
        BlackAccount blackAccount = this.baseMapper.selectOne(wrapper);
        AssertUtil.isNull("黑号不存在", blackAccount);

        blackAccount.setWeight(-1);
        this.baseMapper.updateById(blackAccount);
    }

    @Override
    public Map<String, Object> getBlackAccountList(BlackAccountSearchDTO dto, PageBO pageBO) {
        String limitSql = " limit " + (pageBO.getPage() - 1) + ", " + pageBO.getLimit();
        List<BlackAccountVO> list = this.baseMapper.getBlackAccountList(dto, limitSql);
        Integer count = this.baseMapper.countBlackAccountList(dto, "");

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", list);
        map.put("count", count);

        return map;
    }

    @Override
    public BlackAccountVO searchBlackAccount(BlackAccountSearchDTO dto) {
        String account = dto.getAccount();
        Long gameId = dto.getGameId();
        QueryWrapper<BlackAccount> wrapper = new QueryWrapper<BlackAccount>().eq("account", account);
        if (gameId != null && gameId > 0) {
            wrapper.eq("game_id", gameId);
        }
        BlackAccount blackAccount = this.baseMapper.selectOne(wrapper);
        if (blackAccount != null) {
            BlackAccountVO blackAccountVO = new BlackAccountVO();
            ObjUtil.copy(blackAccount, blackAccountVO);
            return blackAccountVO;
        }
        return null;
    }
}
